﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using EverBuy.SocialNetworking.BusinessObjects;
using EverBuy.SocialNetworking.Data.DataFactory;
using EverBuy.SocialNetworking.Data.Repository.Abstract;
using EverBuy.SocialNetworking.Services.Abstracts;

namespace EverBuy.SocialNetworking.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class AccountService : ServiceBase, IAccountService
    {
        #region repositories
        /// <summary>
        /// 
        /// </summary>
        private IAccountRepository _accountRepository;

        /// <summary>
        /// 
        /// </summary>
        private IPermissionRepository _permissionRepository;
        #endregion

        public AccountService()
        {
            _accountRepository = Factory.AccountRepository;
            _permissionRepository = Factory.PermissionRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool UsernameInUse(string username)
        {
            try
            {
                return _accountRepository.GetAccountByUsername(username) != null;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool EmailInUse(string email)
        {
            try
            {
                return _accountRepository.GetAccountByEmail(email) != null;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Logout()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public string Login(string username, string password)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public EverBuy.SocialNetworking.BusinessObjects.Account GetAccountById(int accountId)
        {
            try
            {
                var account = _accountRepository.GetAccountByID(accountId);
                var permissions = _permissionRepository.GetPermissionsByAccountID(accountId);
                foreach (var permission in permissions)
                {
                    account.Permissions.Add(permission);
                }
                return account;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}
